#include "../Prec.h"


CBSide::CBSide(CBWorld* world,CCPoint &pta,CCPoint &ptb,ESideType sidetype)
  :CBBody(world)
{
  mType = EBT_Side;
  mSideType = sidetype;
  mConnect=NULL;
  mPtA.x=pta.x;
  mPtA.y=pta.y;
  mPtB.x=ptb.x;
  mPtB.y=ptb.y;
  mBallNum=0;
  if(mSideType==EST_TOP)
  {
    float len = abs(pta.x-ptb.x);
    mBallNum = len/CCDefine::Instance()->BallRadius();
    mConnect = new CBBody*[mBallNum];
    memset(mConnect,0,sizeof(CBBody*)*mBallNum);
  }
}


CBSide::~CBSide(void)
{
}

bool CBSide::CheckCollision(CBBody* body)
{
  CBBall* ball = (CBBall*)body;
  float Dis = GetDistance(ball);
  return (ball->mRadius>Dis?true:false);
}


float CBSide::GetDistance(CBBall* ball)
{
  CCPoint v1,v2;
  v1.x = mPtB.x-mPtA.x;
  v1.y = mPtB.y-mPtA.y;
  float v1dis = CCDefine::Instance()->Distance(v1);
  if(v1dis<=0)
    return false;
  v2.x = ball->mCircle.x-mPtA.x;
  v2.y = ball->mCircle.y-mPtB.y;

  float Dis = abs(v1.x*v2.y-v1.y*v2.x)/v1dis;
  return Dis;
}

void CBSide::DisConnect(CBBall* ball)
{
  if(!ball)
    return ;
  for(int i=0;i<mBallNum;i++)
  {
    if(mConnect[i]==ball)
    {
      mConnect[i]=NULL;
      return ;
    }
  }
}

